package cn.bytecollege.controller;

import cn.bytecollege.constant.Resource;
import cn.bytecollege.entity.Page;
import cn.bytecollege.entity.Room;
import cn.bytecollege.service.RoomService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Mr.H
 */
@WebServlet(name = "RoomFindByNumberController",value = "/roomFindByNumber")
public class RoomFindByNumberController extends HttpServlet {
    private RoomService roomService=null;
    public RoomFindByNumberController(){
        roomService=new RoomService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String msg=null;
        //接收参数
        String number=request.getParameter("number");
        //简单验证输入的是否输入数据
        if (number==null||number.equals("")){
            msg="请输入房间号进行查询！";
            request.setAttribute("msg",msg);
            request.getRequestDispatcher("/roomListAll").forward(request,response);
            return;
        }
        Integer roomNumber=Integer.valueOf(number);
        //调用service方法
        List<Room> list=roomService.findRoomByRoomNumber(roomNumber);

        Page<Room> page=new Page<>();
        page.setList(list);

        //封装到请求中
        request.setAttribute("page",page);
        request.getRequestDispatcher(Resource.PAGE_LOCATION+"/roomList.jsp").forward(request,response);
    }
}
